import random
from django.core.cache import cache

class EmailVerificationService:
    @staticmethod
    def generate_code(email):
        code = str(random.randint(100000, 999999))
        cache.set(f'email_verify:{email}', code, timeout=600)  # 10分钟有效期
        return code

    @staticmethod
    def verify_code(email, code):
        cached_code = cache.get(f'email_verify:{email}')
        return cached_code == code

    @staticmethod
    def send_verification_email(email, code):
        from django.core.mail import send_mail
        send_mail(
            subject='您的验证码',
            message=f'您的注册验证码是：{code}，10分钟内有效',
            from_email='noreply@example.com',
            recipient_list=[email],
            fail_silently=False,
        )